from django import forms
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.forms import ModelForm
from django.template import RequestContext
from django.core.mail import EmailMessage

from models import ContatoForm
from configuration.models import FlisolManager, PageData
from inscricoes.models import Participante
from datetime import datetime
from string import Template

import time

from django.contrib.admin.views.decorators import staff_member_required

from contato.models import NotificacaoForm

def contato(request):

    data = PageData()

    if request.method=='POST':
        form = ContatoForm(request.POST)
        if form.is_valid():
            manager = FlisolManager()
            conf = manager.getConf()

            dest = conf.email

            nome = form.cleaned_data['nome']
            email_contado = form.cleaned_data['email']
            telefone = form.cleaned_data['telefone']

            conteudo = "Nome: " + nome + "\n" \
                    + "Email: " + email_contado + "\n" \
                    + "Telefone: " + telefone + "\n" \
                    + "------------------------------------------------\n" \
                    + form.cleaned_data['mensagem']

            subject = nome.title() + " fez contanto pelo site do flisol"

            email = EmailMessage(subject, conteudo, to=[dest])
            email.send()

            form = ContatoForm()
            sucesso = True
        else:
            erro = True
    else:
        form = ContatoForm()
    
    form = form.as_table()
    page = 'contato.html'
    return render_to_response('index.html', locals(),
                              context_instance=RequestContext(request))
@staff_member_required
def notificacao(request):
    if request.method == 'POST':
        manager = FlisolManager()
        conf = manager.getConf()
        dest = conf.email

        form = NotificacaoForm(request.POST)
        if form.is_valid():

            tipo_dest = form.cleaned_data['destinatario']
            titulo = form.cleaned_data['titulo']
            conteudo = form.cleaned_data['mensagem']
            data_hj = datetime.today().strftime('%c')

            if tipo_dest == u'T':
                for i in Participante.objects.all():
                    palestrasInscritas = ''
                    time.sleep(1)
                    for palestra in i.palestras.order_by('horario'):
                        palestrasInscritas += '\t+ ' + palestra.titulo + '\t\t' + str(palestra.horario) + '\n'

                    conteudoCompilado = Template(conteudo).substitute(nome=i.nome, data=data_hj, palestras=palestrasInscritas)
                    email = EmailMessage(titulo, conteudoCompilado, to=[i.email])
                    email.send()
            elif tipo_dest == u'I':
                for i in Participante.objects.filter(instalar=True):
                    palestrasInscritas = ''
                    time.sleep(1)
                    for palestra in i.palestras.order_by('horario'):
                        palestrasInscritas += '\t+ ' + palestra.titulo + '\t\t' + str(palestra.horario) + '\n'

                    conteudoCompilado = Template(conteudo).substitute(nome=i.nome, data=data_hj, palestras=palestrasInscritas)
                    email = EmailMessage(titulo, conteudoCompilado, to=[i.email])
                    email.send()
            return HttpResponse("participantes notificados")
    else:
        form = NotificacaoForm()
        form = form.as_table()
        return render_to_response(
                'admin/contato/notificacao.html', locals(),
                context_instance=RequestContext(request))
    
